﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 6/19/2013
 * Time: 4:21 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Caculator
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		String txtDisplay_OldVal = "0";
		String status = "";
		double so1, so2, kq;
		String phepToan = "";
		
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			status = "Cho nhap so 1";
			so1 = double.MinValue;
			so2 = double.MinValue;
			kq = double.MinValue;
		}
		
		//Cac ham tien ich
		void TinhKQ(){
			if(so1!=double.MinValue && so2!=double.MinValue && phepToan!=""){
				if(phepToan == "/" && so2 == 0)
					throw new Exception("Lỗi chia cho 0");
				switch(phepToan){
					case "+":
						kq = so1+so2;
						break;
					case "-":
						kq = so1-so2;
						break;
					case "*":
						kq = so1*so2;
						break;
					case "/":
						kq = so1/so2;
						break;
					
				}
			}
		}
		
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			Application.Exit();
		}
		
		void CopyToolStripMenuItemClick(object sender, EventArgs e)
		{
			txtDisplay.Copy();
		}
		
		void PasteToolStripMenuItemClick(object sender, EventArgs e)
		{
			txtDisplay.Paste();
		}
		
		void AboutToolStripMenuItemClick(object sender, EventArgs e)
		{
			MessageBox.Show("Caculator v1.0", "About Caculator", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
		}
		
		void BtnSoClick(object sender, EventArgs e)
		{
			Button btnSo = (Button)sender;
			if(status == "Cho nhap so 1" || status == "Dang nhap phep toan" || status == "Tinh ket qua"){
				txtDisplay.Text = btnSo.Text;
				TxtDisplayTextChanged(btnSo, new EventArgs());
				return;
			}
			txtDisplay.Text += btnSo.Text;
			TxtDisplayTextChanged(btnSo, new EventArgs());

		}
		
		void TxtDisplayTextChanged(object sender, EventArgs e)
		{
			if(sender is Button){
				if(status == "Cho nhap so 1" || status == "Dang nhap so 1"){
					status = "Dang nhap so 1";
					so1 = double.Parse(txtDisplay.Text);
					return;
				}
				if(status == "Dang nhap phep toan" || status == "Dang nhap so 2"){
					status = "Dang nhap so 2";
					so2 = double.Parse(txtDisplay.Text);
					return;
				}
				if(status == "Tinh ket qua"){
					status = "Dang nhap so 1";
					so1 = double.Parse(txtDisplay.Text);
					return;
				}
			}
			
			try{
				if(txtDisplay.Text == "")
					txtDisplay.Text = "0";
				double val = double.Parse(txtDisplay.Text);
				if(txtDisplay.Text.Length>1 && txtDisplay.Text.StartsWith("0"))
					txtDisplay.Text = txtDisplay.Text.Remove(0, 1);
				txtDisplay_OldVal = txtDisplay.Text;
			}catch(Exception){
				txtDisplay.Text = txtDisplay_OldVal;
			}
		}
		
		void MainFormKeyPress(object sender, KeyPressEventArgs e)
		{
			Button btnSo = null;
			switch(e.KeyChar){
				case '0':
					btnSo = btnSo0;
					break;
				case '1':
					btnSo = btnSo1;
					break;
				case '2':
					btnSo = btnSo2;
					break;
				case '3':
					btnSo = btnSo3;
					break;
				case '4':
					btnSo = btnSo4;
					break;
				case '5':
					btnSo = btnSo5;
					break;
				case '6':
					btnSo = btnSo6;
					break;
				case '7':
					btnSo = btnSo7;
					break;
				case '8':
					btnSo = btnSo8;
					break;
				case '9':
					btnSo = btnSo9;
					break;
				case '.':
					btnSo = btnDot;
					break;
				case (char)Keys.Back:
					BtnBackClick(btnBack, new EventArgs());
					break;
				case '+':
					BtnPhepToanClick(btnPlus, new EventArgs());
					break;
				case '-':
					BtnPhepToanClick(btnSub, new EventArgs());
					break;
				case '=':
				case (char)Keys.Enter:
					BtnEqualClick(btnEqual, new EventArgs());
					break;
				case (char)Keys.Escape:
					BtnCClick(btnC, new EventArgs());
					break;
			}
			if(btnSo != null)
				BtnSoClick(btnSo, new EventArgs());
		}
		
		void BtnMCClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnMRClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnMSClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnMPlusClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnMSubClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnBackClick(object sender, EventArgs e)
		{
			txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.Text.Length-1);
		}
		
		void BtnCEClick(object sender, EventArgs e)
		{
			txtDisplay.Text = "0";
		}
		
		void BtnCClick(object sender, EventArgs e)
		{
			txtDisplay.Text = "0";
			txtPhepToan.Text = "";
			so1 = so2 = kq = double.MinValue;
			status = "Cho nhap so 1";
		}
		
		void BtnSignClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnSQRTClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnPercentClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnInverseClick(object sender, EventArgs e)
		{
			
		}
		
		void BtnPhepToanClick(object sender, EventArgs e)
		{
			Button btnPhepToan = (Button)sender;
			if(status == "Cho nhap so 1"){
				so1 = 0;
				phepToan = btnPhepToan.Text;
				txtPhepToan.Text = phepToan;
				status = "Dang nhap phep toan";
				return;
			}
			if(status == "Dang nhap so 1"){
				phepToan = btnPhepToan.Text;
				txtPhepToan.Text = phepToan;
				status = "Dang nhap phep toan";
				return;
			}
			if(status == "Dang nhap phep toan"){
				phepToan = btnPhepToan.Text;
				txtPhepToan.Text = phepToan;
				status = "Dang nhap phep toan";
				return;
			}
			if(status == "Dang nhap so 2"){
				try{
					TinhKQ();
					so1 = kq;
					txtDisplay.Text = kq.ToString();
					phepToan = btnPhepToan.Text;
					txtPhepToan.Text = phepToan;
					status = "Dang nhap phep toan";
				}catch(Exception ex){
					MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
				}
				return;
			}
			if(status == "Tinh ket qua"){
				phepToan = btnPhepToan.Text;
				txtPhepToan.Text = phepToan;
				so2 = double.MinValue;
				status = "Dang nhap phep toan";
				return;
			}
		}
		
		void BtnEqualClick(object sender, EventArgs e)
		{
			if(status == "Cho nhap so 1") return;
			if(status == "Dang nhap so 1"){
				if(so2 == double.MinValue || phepToan == "")
					kq = so2 = so1;
				else{
					try{
						TinhKQ();
						so2 = so1;
						so1 = kq;
					}catch(Exception ex){
						MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
					}
				}
				txtDisplay.Text = kq.ToString();
				status = "Tinh ket qua";
				return;
			}
			if((status == "Dang nhap phep toan" || status == "Dang nhap so 2" || status == "Tinh ket qua") && phepToan!=""){
				if(status == "Dang nhap phep toan" && so2 == double.MinValue)
					so2 = so1;
				try{
					TinhKQ();
					so1 = kq;
					txtDisplay.Text = kq.ToString();
					status = "Tinh ket qua";
				}catch(Exception ex){
					MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
				}
				return;
			}
		}
	}
}
